//波次单
$(function(){
	//搜索
	$('.searchBar').click(function(){
		var thisform=$(this).parent('form');
		var order_no=thisform.find('input[name=order_no]').val();
		var user_name=thisform.find('input[name=user_name]').val();
		var logistics_no=thisform.find('input[name=logistics_no]').val();
		var is_delivery=thisform.find('input[name=is_delivery]').val();
		var logiscompany_name=thisform.find('input[name=logiscompany_name]').val();
		var start_time=thisform.find('input[name=start_time]').val();
		var end_time=thisform.find('input[name=end_time]').val();
		$('#financeDatagrid').datagrid('options').pageNumber=1;//设置页码初始值为1
		$('#financeDatagrid').datagrid({
			queryParams:{
				logistics_no:logistics_no,
				order_no:order_no,
				user_name:user_name,
				is_delivery:is_delivery,
				start_time:start_time,
				end_time:end_time,
				logiscompany_name:logiscompany_name
			}
		});
	})

	//更新行数据
	function financeUpdateActions(value){
		$.post(financeEditUrl,{id:value.id,finance_goods_tax:value.finance_goods_tax,finance_factory_price:value.finance_factory_price,finance_total_price:value.finance_total_price},function(result){
            if (!result.status){
                $.messager.alert('错误提示',result.message,'error');
            }
        },'json').error(function(data){
        	var info=eval('('+data.responseText+')');
			$.messager.confirm('错误提示',info.message,function(r){
				//$('#authAccessSetDialog').dialog('close');
			});
        });
	};

	var editIndex = undefined;
	function endEditing(){
		if (editIndex == undefined){return true}
		if ($('#financeDatagrid').datagrid('validateRow', editIndex)){
			$('#financeDatagrid').datagrid('endEdit', editIndex);
			editIndex = undefined;
			return true;
		} else {
			return false;
		}
	};
	//双击行编辑
	function onDblClickRow(index){
		if (editIndex != index){
			if (endEditing()){
				$('#financeDatagrid').datagrid('selectRow', index);
				$('#financeDatagrid').datagrid('endEdit', editIndex);
				$('#financeDatagrid').datagrid('beginEdit', index);
				editIndex = index;
			} else {
				$('#financeDatagrid').datagrid('selectRow', editIndex);
			}
		}
	};
	$('#financeDatagrid').datagrid({
		url:getOrderlistsUrl,
		title:'订单列表',
		fitColumns:true,
		rownumbers:true,
		pageSize:25,
		pageNumber:1,
		pageList:[5,10,15,20,25,30,40,50,100],
		columns:[[
		{field:'id',title:'id',checkbox:true},
		{field:'order_no',title:'订单编码',sortable:true},
		{field:'user_name',title:'客户姓名'},
		{field:'platform_name',title:'订单来源'},
		{field:'logistics_no',title:'物流单号'},
		{field:'logiscompany_name',title:'物流企业名称'},
		{field:'add_time',title:'推送时间'},
		{field:'total_price',title:'销售价',
				formatter:function(val,rowData,rowIndex){//保留两位小数
			        if(val!=null){
			            return Math.round(val*100)/100;
					}
			    }
		},
		{field:'finance_total_price',title:'财务编辑销售价',
				formatter:function(val,rowData,rowIndex){
			        if(val!=null){
			            return Math.round(val*100)/100;
					}
			    },
				editor:{
					type:'numberbox',
					options:{
						required: true,  
						missingMessage: '不能为空',
						min:1
					}
				}
		},
		{field:'goods_tax',title:'税金',
				formatter:function(val,rowData,rowIndex){
			        if(val!=null){
			            return Math.round(val*100)/100;
					}
			    }
		},
		{field:'finance_goods_tax',title:'财务编辑税金',
				formatter:function(val,rowData,rowIndex){
			        if(val!=null){
			            return Math.round(val*100)/100;
					}
			    },
				editor:{
					type:'numberbox',
					options:{
						required: true,  
						missingMessage: '不能为空'
					}
				}
		},
		{field:'factory_price',title:'成本价',
				formatter:function(val,rowData,rowIndex){
			        if(val!=null){
			            return Math.round(val*100)/100;
					}
			    }
		},
		{field:'finance_factory_price',title:'财务编辑成本价',
				formatter:function(val,rowData,rowIndex){
			        if(val!=null){
			            return Math.round(val*100)/100;
					}
			    },
				editor:{
					type:'numberbox',
					options:{
						required: true,  
						missingMessage: '不能为空',
						min:1
					}
				}
		},
		{field:'is_delivery',title:'已出库',
			formatter:function(value,row,index){
				if (value==0){
					return "否";
				} else if(value==1) {
					return "是";
				}else if(value='2'){
					return "";
				}
			}
		}
		]],
		onDblClickRow: onDblClickRow,
		onAfterEdit:function(index,row){
			//console.log(row);
					row.editing = false;
					financeUpdateActions(row);
		},
		onLoadError:function(data){
			var info=eval('('+data.responseText+')');
			$.messager.confirm('错误提示',info.message,function(r){
				var tab=$('#Tabs').tabs('getSelected');
				var index=$('#Tabs').tabs('getTabIndex',tab);
				$('#Tabs').tabs('close',index);
			});
		},
		rowStyler:function(index,row){
			if (row.color==0){
				return 'background-color:pink;color:blue;font-weight:bold;';
			}
		},
		pagination:true,
		fit:true,
		showFooter: true,
	});
});
financeObj={
	outboundBar:function(){	//出库
		var financeRow=$('#financeDatagrid').datagrid('getSelections');
        if(financeRow.length<1){
			$.messager.alert('错误提示','请选择列表数据','error');
		}else{
			$.messager.confirm('提示','确认出库吗！',function(r){
				if(r){
					var ids=[];
					for(var i=0;i<financeRow.length;i++){
						ids.push(financeRow[i].id);
					}
					var id=ids.join(',');
					$.post(outboundOrderUrl,{id:id},function(result){
						if (!result.status){
							$.messager.alert('错误提示',result.message,'error');
							$('#financeDatagrid').datagrid('reload');
						}else{
							$.messager.alert('提示',result.message,'info');
							$('#financeDatagrid').datagrid('reload');
						}
					},'json').error(function(data){
						var info=eval('('+data.responseText+')');
						$.messager.confirm('错误提示',info.message,function(r){
							//$('#authAccessSetDialog').dialog('close');
						});
					});
				}
			});
        }
	},
	showAll:function(){
		$('#financeDatagrid').datagrid({ queryParams:''});
	},
	editBar:function(){//显示编辑框
		var financeRow=$('#financeDatagrid').datagrid('getSelections');
        if (financeRow.length>1){		            	
            $.messager.alert('提示','一次只能修改一条记录!','info');
        }else if(financeRow.length==1){            	
        	$('#financeEditDialog').dialog('open').dialog('setTitle','编辑用户');
            $('#financeEditForm').form('load',financeRow[0]);
        }else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	reloadBar:function(){//刷新数据
		$('#financeDatagrid').datagrid('reload');
	},
	saveBar:function(){//保存数据
		var editIndex = undefined;
		if (editIndex == undefined){endEditing = 1;}
		if ($('#financeDatagrid').datagrid('validateRow', editIndex)){
			$('#financeDatagrid').datagrid('endEdit', editIndex);
			editIndex = undefined;
			endEditing = 1;
		} else {
			endEditing = 0;
		}
		if (endEditing == 1){
			$('#financeDatagrid').datagrid('acceptChanges');
		}
	},
	save:function(url){//编辑后保存数据
		$('#financeEditForm').form('submit',{
            url: url,
            onSubmit: function(){
                return $(this).form('validate');
            },
            success: function(result){
            	 var result=eval('('+result+')');
                if (!result.status){
                    $.messager.confirm('错误提示',result.message,function(r){
                    	$('#financeEditDialog').dialog('close');
                    });
                }else{
                    $('#financeEditDialog').dialog('close');      
                    $('#financeDatagrid').datagrid('reload');    
                }
            }
        });
	},
	excelBar:function(){//导出excel
		var thisform=$('#financeSearchForm');
		// console.log(thisform);
		var order_no=thisform.find('input[name=order_no]').val();
		var user_name=thisform.find('input[name=user_name]').val();
		var logistics_no=thisform.find('input[name=logistics_no]').val();
		var is_delivery=thisform.find('input[name=is_delivery]').val();
		url=getOrderlistsUrl+"&excel=1&rows=6000";
		if(order_no!=''){
			url=url+"&order_no="+order_no;
		}
		if(user_name!=''){
			url=url+"&user_name="+user_name;
		}
		if(logistics_no!=''){
			url=url+"&logistics_no="+logistics_no;
		}
		if(is_delivery!=0){
			url=url+"&is_delivery="+is_delivery;
		}
		window.location.href=url;
	}
}